<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>使用正则表达式</title>
  <link href="../Styles/styles.css" rel="stylesheet" type="text/css"/>
</head>

<body>
  <h2>正则表达式参考</h2>

  <p class="h2subheading">— Regex 参考 —</p>

  <p>This tutorial is basically a quick reference for Regex.</p>
  <p>本教程基本上是 Regex 的快速参考。</p>

  <p>Regex is a very powerful way for you to edit and cleanup your EPUB when using Find &amp; Replace. However, when you first start using regex it can be confusing because there are a lot of commands you can use. This is a brief summary of some basic regex commands.</p>
  <p>正则表达式是使用“查找 &amp; 替换”时编辑和清除 EPUB 的一种非常强大的方法。但是，当您第一次开始使用正则表达式时，可能会感到困惑，因为您可以使用很多命令。这是一些基本正则表达式命令的简短摘要。</p>

  <p>You can find more details about regex at these links (or ask for help in the Sigil forum):</p>
  <p>您可以在以下链接中找到有关正则表达式的更多详细信息（或在 Sigil 论坛中寻求帮助）：</p>

  <ul>
    <li><a href="http://www.mobileread.com/forums/showthread.php?t=118569">Regex overview at MobileRead</a></li>

    <li><a href="https://www.regular-expressions.info/tutorial.html">An Online Regex tutorial/reference</a></li>

    <li><a href="http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/">A one page command summary</a></li>

    <li><a href="http://docs.python.org/library/re.html">Regex as used by Python</a></li>

    <li><a href="http://www.pcre.org/pcre.txt">Official PCRE details – start at the PCRESYNTAX(3) section</a></li>
  </ul>

  <h3 class="sigil_not_in_toc" id="regex_overview">总览</h3>

  <p class="sigilNotInTOC">When using regex commands in Find &amp; Replace the purpose is to allow you to search for patterns instead of exact text. This means that you can tell Find &amp; Replace to search for "a number" instead of "8", or to match any words between certain tags and then replace it with new tags or new words.</p>
  <p class="sigilNotInTOC">在“查找 &amp; 替换”中使用正则表达式命令时，目的是允许您搜索模式而不是确切的文本。这意味着您可以告诉“查找 &amp; 替换”搜索“数字”而不是“8”，或者匹配某些标签之间的任何单词，然后将其替换为新标签或新单词。</p>

  <p>In order to allow you to search for patterns, you need to use regex commands in place of the specific text you want in your search. For instance, instead of searching for <span class="example">8</span> you could search for <span class="example">\d</span> (which means one digit) or <span class="example">\d+</span> (which means one or more digits).</p>
  <p>为了允许您搜索模式，您需要使用正则表达式命令来代替您想要在搜索中使用的特定文本。例如，除了搜索 <span class="example">8</span> 外，您可以搜索 <span class="example">\d</span>（表示一位数字）或 <span class="example">\d+</span>（表示一位或多位数字）。</p>

  <p>An example Find &amp; Replace command using regex might be to change all level 1 headings to level 2 headings:</p>
  <p>使用正则表达式的“查找 &amp; 替换”命令示例可能是将所有 1 级标题更改为 2 级标题：</p>

  <ul>
    <li><span class="listheading">查找：</span> <span class="example">&lt;h1(.*?)&lt;/h1&gt;</span></li>

    <li><span class="listheading">替换：</span> <span class="example">&lt;h2\1&lt;/h2&gt;</span></li>
  </ul>

  <p>where the Find command searches for any text between the h1 start and end tags, and using () to save the text. (The start tag is left open in order to catch any class definitions, and the ? tells .* to do minimal matching). The Replace command is used to replace the matched text with the saved text (indicated by \1) surrounded by the h2 start and end tags.</p>
  <p>“查找”命令在其中搜索 h1 开始标记和结束标记之间的任何文本，并使用 () 保存文本。（开始标记保持打开状态以捕获任何类定义，并且符号 ? 告诉 .* 进行最小匹配）。“替换”命令用于将匹配的文本替换为保存的文本（由 \1 表示），该文本由h2开始和结束标记包围。</p>

  <p>The following sections give a sample of common regex commands that you may find useful.</p>
  <p>以下各节提供了一些常见的 regex 命令示例，您可能会发现它们很有用。</p>

  <h3 class="sigil_not_in_toc" id="regex_modifiers">通用修饰符：</h3>

  <p>You can put these commands at the start of any regex to modify how the expression behaves:</p>
  <p>您可以将这些命令放在任何正则表达式的开头，以修改表达式的行为：</p>

  <ul>
    <li><span class="example">(?s)</span> – search across lines when using <span class="example">.*</span> since by default <span class="example">.*</span> only matches within a line of text. This is done by the DotAll option in Find &amp; Replace.</li>
    <li><span class="example">(?s)</span>——使用 <span class="example">.*</span> 时跨行搜索，因为默认情况下 <span class="example">.*</span> 仅在一行文本中匹配。这是通过“查找 &amp; 替换”中的 DotAll 选项完成的。</li>

    <li><span class="example">(?U)</span> – match the first occurrence of your string (also called minimal or ungreedy matching). This is very useful when using <span class="example">.*</span> to avoid matching too much. This is done by the Minimal Match option in Find &amp; Replace.</li>
    <li><span class="example">(?U)</span>——匹配字符串的第一次出现（也称为最小匹配或不匹配匹配）。当使用 <span class="example">.*</span> 以避免过多匹配时，这非常有用。这是通过“查找 &amp; 替换”中的“最小匹配”选项完成的。</li>

    <li><span class="example">(?i)</span> – ignore case when matching.</li>
    <li><span class="example">(?i)</span>——匹配时忽略大小写。</li>
  </ul>

  <p>You can combine modifiers, e.g. <span class="example">(?sU)</span> is commonly used.</p>
  <p>您可以组合修饰符，例如通常使用 <span class="example">(?sU)</span>。</p>

  <h3 class="sigil_not_in_toc">单个字符：</h3>

  <p>You can use these regex commands to represent single characters:</p>
  <p>您可以使用以下正则表达式命令来表示单个字符：</p>

  <ul>
    <li><span class="example">.</span> – matches one character</li>
    <li><span class="example">.</span> – 匹配一个字符</li>

    <li><span class="example">\d</span> – match one digit</li>
    <li><span class="example">\d</span> – 匹配一个数字</li>

    <li><span class="example">\t</span> – match one tab</li>
    <li><span class="example">\t</span> – 匹配一个 tab</li>

    <li><span class="example">[abc]</span> – any character that is a b or c</li>
    <li><span class="example">[abc]</span> – a 或 b 或 c 的任何字符</li>

    <li><span class="example">[^abc]</span> – any character that is NOT a b or c</li>
    <li><span class="example">[^abc]</span> – 不是 a 或 b 或 c 的任何字符</li>

    <li><span class="example">[0-9]</span> – any digit from 0 to 9</li>
    <li><span class="example">[0-9]</span> – 0到9之间的任何数字</li>

    <li><span class="example">[:alpha:]</span> – any letter, upper or lower case</li>
    <li><span class="example">[:alpha:]</span> – 任何字母，大写或小写</li>

    <li><span class="example">[^:alnum:]</span> – any character that is NOT a digit or upper or lowercase letter</li>
    <li><span class="example">[^:alnum:]</span> – 不是数字、大写或小写字母的任何字符</li>
  </ul>

  <h3 class="sigil_not_in_toc">多个字符：</h3>

  <p>These regex quantifiers or codes allow you to represent multiple characters:</p>
  <p>这些正则表达式量词或代码使您可以表示多个字符：</p>

  <ul>
    <li><span class="example">*</span> – use after a character to mean 0 or more, e.g. <span class="example">.*</span> means 0 or more of any character</li>
    <li><span class="example">*</span> – 在一个字符后使用以表示 0 或更多，例如 <span class="example">.*</span> 表示任何字符中的 0 个或多个</li>

    <li><span class="example">+</span> – use after a character to mean 1 or more, e.g. <span class="example">\d+</span> means 1 or more digits</li>
    <li><span class="example">+</span> – 在一个字符后使用表示 1 个或多个，例如 <span class="example">\d+</span> 表示 1 个或多个数字</li>

    <li><span class="example">?</span> – use after a character to mean 0 or 1 instance, e.g. \d? means none or 1 digit</li>
    <li><span class="example">?</span> – 在一个字符后使用以表示 0 或 1 个实例，例如 \d? 表示无或一位数字</li>

    <li><span class="example">\s</span> – matches whitespace, including space, tab, end of line carriage returns or newlines</li>
    <li><span class="example">\s</span> – 匹配空白，包括空格、制表符、换行符结尾或换行符</li>
  </ul>

  <p>And there is a special character that tells your search to stop at the first match it finds instead of the largest match which you can use instead of the general modifier <span class="example">(?U)</span>:</p>
  <p>并且有一个特殊字符告诉您搜索在找到的第一个匹配项处停止，而不是您可以代替通用修饰符 <span class="example">(?U)</span> 使用的最大匹配项：</p>

  <ul>
    <li><span class="example">?</span> – use after a quantifier to do a minimal search, e.g. <span class="example">.*?</span> or <span class="example">\d+?</span></li>
    <li><span class="example">?</span> – 在量词后使用进行最小搜索，例如 <span class="example">.*?</span> 或 <span class="example">\d+?</span></li>
  </ul>

  <h3 class="sigil_not_in_toc">分组：</h3>

  <p>In some cases you need to group your expressions together so that, for example, you can save them for a later replace, or to indicate a choice of words:</p>
  <p>在某些情况下，您需要将表达式分组在一起，例如，您可以保存它们以备以后替换，或表示选择单词：</p>

  <ul>
    <li><span class="example">(atext|btext)</span> – atext or btext</li>
    <li><span class="example">(atext|btext)</span> – atext 或 btext</li>

    <li><span class="example">(group)</span> – group the words together for later retrieval – you can use multiple groups</li>
    <li><span class="example">(group)</span> – 将单词分组在一起以便以后检索——您可以使用多个分组</li>
  </ul>

  <h3 class="sigil_not_in_toc">替换</h3>

  <p>When replacing text, one of the most useful features is to be able to use text that was matched by storing it in a group. Once you have grouped text you can reference it in your replace command:</p>
  <p>替换文本时，最有用的功能之一是能够使用通过分组存储而匹配的文本。将文本分组后，可以在您的替换命令中引用它：</p>

  <ul>
    <li><span class="example">\1</span> – use in Replace to retrieve the value of a saved group (use \2 for the second group, etc.)</li>
    <li><span class="example">\1</span> – 在替换中使用来检索已保存组的值（第二组使用 \2，依此类推）</li>
  </ul>
</body>
</html>